#include <Arduino.h>
#include "Syslog_ced.h"
#include <Ethernet.h>
#include <EthernetUdp.h>

EthernetUDP SyslogUdp;

IPAddress r_pi(192, 168, 1, 5);
IPAddress ip_relai(192, 168, 1, 3);

#define SYSLOG_DEFAULT_PORT 514
#define def_local_syslog 8889

//8 * facility + severity  16/5

void syslog(byte severity, const char * tosend){
  //Serial.println("beg syslog");
  if (severity <= SYSLOG_level)
  {
    String Pri=String("<");
    Pri+=(8 * 16 + severity);
    Pri+=">arduino ";

    char UDPBufferPri[Pri.length()+1];
    Pri.toCharArray(UDPBufferPri,Pri.length()+1);

    if (SyslogUdp.begin(def_local_syslog))
    {
      //Serial.println("send syslog");
      SyslogUdp.beginPacket(r_pi, SYSLOG_DEFAULT_PORT);
      SyslogUdp.write(UDPBufferPri);
      SyslogUdp.write(tosend);
      SyslogUdp.endPacket();
    }
    SyslogUdp.stop();
  }
}

void syslog(byte severity, String tosend){
  //Serial.println("beg syslog");
  if (severity <= SYSLOG_level)
  {
    String Pri=String("<");
    Pri+=(8 * 16 + severity);
    Pri+=">arduino ";

    char UDPBufferPri[Pri.length()+1];
    Pri.toCharArray(UDPBufferPri,Pri.length()+1);
    if (SyslogUdp.begin(def_local_syslog))
    {
      // Serial.println("send syslog");
      SyslogUdp.beginPacket(r_pi, SYSLOG_DEFAULT_PORT);
      SyslogUdp.write(UDPBufferPri);
      char UDPBufferPri[tosend.length()+1];
      tosend.toCharArray(UDPBufferPri,tosend.length()+1);
      SyslogUdp.write(UDPBufferPri);
      SyslogUdp.endPacket();

    }
    SyslogUdp.stop();
    //Serial.println("end syslog");
  }

}



int send_syslog_relai(byte relai1, byte relai2)
{
  String Pri=String();
  char text[10]  ;
  snprintf(text,10,"%d:%d",relai1,relai2);
  if (SyslogUdp.begin(def_local_syslog))
  {
    //Serial.println("send syslog");
    SyslogUdp.beginPacket(ip_relai, SYSLOG_DEFAULT_PORT);
    SyslogUdp.write(text);
    SyslogUdp.endPacket();
  }  
  SyslogUdp.stop();
  return(0);
}








